/**
 * 
 * Praktikum CG WP WS13/14
 * Gruppe: Andreas Rebri(andreas.rebri@haw-hamburg.de)
 * 		   Joschka Schulz(joschka.schulz@haw-hamburg.de)
 * Aufgabe : Aufgabenblatt 6
 * Verwendete Quellen: -
 */
package cg6;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *The Slider wrapper class to implement the slider
 */
public class SliderWrapper extends JPanel
        implements
        ChangeListener {

	/**
	 * the minimal value of the slider
	 */
    static final int MIN_VAL = 0;
    
    /**
     * the maximal value of the slider
     */
    static final int MAX_VAL = 100;
    
    /**
     * the initial value of the slider
     */
    static final int INIT_VAL = 0;
    
    /**
     * a reference to the class of Aufgabe6
     */
    private Aufgabe6 aufgabe6Referenz;

    /**
     * the constructor of the class with a reference of the class Aufgabe6
     * 
     * @param aufgabe6refernz the reference of the class Aufgabe6
     */
    public SliderWrapper(Aufgabe6 aufgabe6refernz) {
        this.aufgabe6Referenz = aufgabe6refernz;

        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));


        //Create the label.
        JLabel sliderLabel = new JLabel("Value", JLabel.CENTER);
        sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        //Create the slider.
        JSlider slider = new JSlider(JSlider.HORIZONTAL,
                MIN_VAL, MAX_VAL, INIT_VAL);


        slider.addChangeListener(this);

        //Turn on labels at major tick marks.
        slider.setBorder(
                BorderFactory.createEmptyBorder(0, 0, 10, 0));
        Font font = new Font("Serif", Font.ITALIC, 15);
        slider.setFont(font);

        //Put everything together.
        add(sliderLabel);
        add(slider);
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    /**
     * Listen to the slider.
     */
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider) e.getSource();
        double val = (double)source.getValue()/100;
        
        aufgabe6Referenz.movePoint(val);
    }


    /**
     * Create the GUI and show it.
     */
    public void createAndShowGUI(Container pane) {
        //Creates a new SliderWrapper
        SliderWrapper animator = new SliderWrapper(aufgabe6Referenz);

        //Add content to the window.
        pane.add("South", animator);
        
        //revalidate the pane
        pane.revalidate();
    }
}
